package com.fragrance.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fragrance.service.UserService;
import com.fragrance.service.impl.UserServiceImpl;
import com.fragrance.util.Constants;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Redis;

public class UserInnerController  extends Controller {

	private Logger logger = LoggerFactory.getLogger(UserInnerController.class);
	
	private UserService userService = new UserServiceImpl();
	
	/**
	 * 运营管理后台-获取用户菜单信息
	 * @param request
	 * @param response
	 * @return
	 */
	public void getOmsMenu(){
		logger.info("--begin RoleController.getMenu currentTimeMillis="+System.currentTimeMillis());
		Map<String, Object> infoMap = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			
			//获取参数
			String token = getPara("token");
			String userInfoStr = Redis.use("users").get(token);
			JSONObject userInfo = JSONObject.parseObject(userInfoStr);
			
			//获取所有一级菜单
			JSONArray menuArray =  Redis.use("users").get("top_menu_" + userInfo.getString("customer_id"));
			logger.debug("-- in RoleController.getMenu,topMenu="+menuArray);
			
			for(int i=0;i<menuArray.size();i++){
				JSONObject menu = JSONObject.parseObject(menuArray.get(i).toString());
				String menuId= menu.getString("id");
				JSONArray childMenuArray =  Redis.use("users").get("m_" + menuId + "_" + userInfo.getString("customer_id"));
				logger.debug("-- in RoleController.getMenu,topMenuId="+menuId+",childMenu="+childMenuArray);
				if(null!=childMenuArray && childMenuArray.size()>0){
					JSONObject childMenuObj = childMenuArray.getJSONObject(0);
					menu.put("childNode", childMenuObj.get("childNode"));
				}
				menuArray.set(i, menu);
			}
			infoMap.put("menuList", menuArray);
			infoMap.put("userName", userInfo.get("user_name"));
			infoMap.put("userId", userInfo.get("id"));
			infoMap.put("customerId", userInfo.get("customer_id"));
			
			resultMap.put("code", "000000");
			resultMap.put("msg", "成功");
			resultMap.put("data", infoMap);
		} catch (Exception e) {
			logger.error("--error in RoleController.getMenu code:00001,msg:运营管理后台-获取用户菜单信息异常",e);
			resultMap.put("code", "000001");
			resultMap.put("msg", "系统繁忙，请稍后再试！");
		}
		logger.info("--end RoleController.getMenu currentTimeMillis="+System.currentTimeMillis());
		renderJson(resultMap);
	}
	
	public void loginOut() {
		logger.info("--begin LoginOutController.loginOut currentTimeMillis="+System.currentTimeMillis());
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String token = getPara("token");
		String userInfoStr = Redis.use("users").get(token);
		JSONObject userInfo = JSONObject.parseObject(userInfoStr);
		logger.info("userInfo = " + userInfo);
		try {
			if(StringUtils.isEmpty(token)){
				resultMap.put("code",Constants.ERROR_CODE_3);
				resultMap.put("Msg", Constants.ERROR_MSG_3);
				renderJson(resultMap);
			}else{
				String tokenValue = Redis.use("users").get(token);
				Redis.use("users").del(tokenValue);
				resultMap.put("code",Constants.SUCCESS_CODE);
				resultMap.put("Msg", Constants.SUCCESS_MSG);
				renderJson(resultMap);
			}
			
		} catch (Exception e) {
			logger.error("--error LoginOutController.loginOut,退出系统异常",e);
			resultMap.put("code", "100001");
			resultMap.put("msg", "退出系统失败");
		}
		logger.info("--end LoginOutController.loginOut currentTimeMillis="+System.currentTimeMillis());
		renderJson(resultMap);

	}

	public void userList() {
		logger.info("--begin UserInnerController.userList currentTimeMillis="+System.currentTimeMillis());
		Map<String,Object> params = new HashMap<String,Object>();
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String name = getPara("name");
		String mobileNo = getPara("mobileNo");
		String pageSize = getPara("pageSize");
		String pageNum = getPara("pageNum");
		
		try {
			params.put("name", name);
			params.put("mobileNo", mobileNo);
			params.put("pageSize", StringUtils.isNotBlank(pageSize)?pageSize:"20");
			params.put("pageNum", StringUtils.isNotBlank(pageNum)?pageNum:"1");
			logger.info("-- UserInnerController.userList params={}",params);
			resultMap = userService.userList(params);
			
		} catch (Exception e) {
			logger.error("-- error UserInnerController.userList,Exception{}",e);
			resultMap.put("code", Constants.ERROR_CODE);
			resultMap.put("msg", Constants.ERROR_MSG);
		}
		logger.info("--end UserInnerController.userList currentTimeMillis="+System.currentTimeMillis());
		renderJson(resultMap);
	}

	public void userDetail() {
		logger.info("--begin UserInnerController.userDetail currentTimeMillis="+System.currentTimeMillis());
		Map<String, Object> resultMap = new HashMap<String,Object>();
		String userId = getPara("userId");
		try {
			if(userId == null || userId ==""){
				resultMap.put("code", "10000401");
				resultMap.put("msg", "输入的用户id不能为空");
				renderJson(resultMap);
				return;
			}
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("userId", userId);
			logger.info("-- UserInnerController.userDetail params={}",params);
			resultMap = userService.userDetail(params);
			
		} catch (Exception e) {
			logger.error("-- error UserInnerController.userDetail,Exception{}",e);
			resultMap.put("code", Constants.ERROR_CODE);
			resultMap.put("msg", Constants.ERROR_MSG);
		}
		logger.info("--end UserInnerController.userDetail currentTimeMillis="+System.currentTimeMillis());
		renderJson(resultMap);

	}

	public void userEdit() {
		logger.info("--begin UserInnerController.userEdit currentTimeMillis="+System.currentTimeMillis());
		String userInfo = Redis.use("users").get(getPara("token"));
		JSONObject userInfoJson = JSONObject.parseObject(userInfo);
		
		Map<String, Object> resultMap = new HashMap<String,Object>();
		String userId = getPara("userId");
		String name = getPara("name");
		String mobileNo = getPara("mobileNo");
		String sex = getPara("sex");
		String contactNo = getPara("contactNo");
		String email = getPara("email");
		String birthday = getPara("birthday");
		/*if(!StringUtils.isEmpty(birthday)){
			StringBuilder str = new StringBuilder(birthday);
			str.insert(4, "-");
			str.insert(7, "-");
			birthday = str.toString();
		}*/
		//String customerId = getPara("customerId");
		
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("name", name);
			params.put("mobileNo", mobileNo);
			params.put("sex", sex);
			params.put("contactNo", contactNo);
			params.put("email", email);
			params.put("birthday", birthday);
			params.put("updateId", userInfoJson.get("id"));
			params.put("userId", userId);
			//params.put("customerId", customerId);
			
			logger.info("--in UserInnerController.userEdit params={}",params);
			resultMap = userService.userEdit(params);
			
		} catch (Exception e) {
			logger.error("--error UserInnerController.userEdit,Exception{}",e);
			resultMap.put("code", Constants.ERROR_CODE);
			resultMap.put("msg", Constants.ERROR_MSG);
		}
		logger.info("--end UserInnerController.userEdit currentTimeMillis="+System.currentTimeMillis());
		renderJson(resultMap);

	}

	public void userAdd() {
		logger.info("--begin UserInnerController.userAdd currentTimeMillis="+System.currentTimeMillis());
		String userInfo = Redis.use("users").get(getPara("token"));
		JSONObject userInfoJson = JSONObject.parseObject(userInfo);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String accountNo = getPara("accountNo");
		String password = getPara("password");
		String contactNo = getPara("contactNo");
		String mobileNo = getPara("mobileNo");
		String userName = getPara("userName");
		String sex = getPara("sex");
		String idType = getPara("idType");
		String idNo = getPara("idNo");
		String birthday = getPara("birthday");
		String email = getPara("email");
		
		if(!StringUtils.isEmpty(birthday)){
			StringBuilder str = new StringBuilder(birthday);
			str.insert(4, "-");
			str.insert(7, "-");
			birthday = str.toString();
		}
		
		if(accountNo == null || accountNo == ""){
			resultMap.put("code", "10000701");
			resultMap.put("msg", "输入的账号号不能为空");
			renderJson(resultMap);
			return;
		}
		if(password == null || password == ""){
			resultMap.put("code", "10000702");
			resultMap.put("msg", "输入的密码不能为空");
			renderJson(resultMap);
			return;
		}
		if(mobileNo == null || mobileNo == ""){
			resultMap.put("code", "10000703");
			resultMap.put("msg", "输入的手机号不能为空");
			renderJson(resultMap);
			return;
		}
		if(userName == null || userName == ""){
			resultMap.put("code", "10000704");
			resultMap.put("msg", "输入的用户名不能为空");
			renderJson(resultMap);
			return;
		}
		
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("accountNo", accountNo);
			params.put("password", password);
			params.put("contactNo", contactNo);
			params.put("mobileNo", mobileNo);
			params.put("userName", userName);
			params.put("sex", sex);
			params.put("idType", idType);
			params.put("idNo", idNo);
			params.put("birthday", birthday);
			params.put("email", email);
			params.put("createId", userInfoJson.get("id"));
			
			logger.info("--in UserInnerController.userAdd  params={}",params);
			resultMap = userService.userAdd(params);
			
		} catch (Exception e) {
			logger.error("--error UserInnerController.userAdd,Exception{}",e);
			resultMap.put("code", Constants.ERROR_CODE);
			resultMap.put("msg", Constants.ERROR_MSG);
		}  
		logger.info("--end UserInnerController.userAdd currentTimeMillis="+System.currentTimeMillis());
		 renderJson(resultMap);
	}

	public void userDelete() {
		logger.info("--begin UserInnerController.userDelete currentTimeMillis="+System.currentTimeMillis());
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String userInfo = Redis.use("users").get(getPara("token"));
		JSONObject userInfoJson = JSONObject.parseObject(userInfo);
		
		String userId = getPara("userId");
		
		if(userId == null || userId ==""){
			resultMap.put("code", "10000601");
			resultMap.put("msg", "输入的用户Id不能为空");
			renderJson(resultMap);
			return;
		}
		try {
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("userId", userId);
			params.put("updateId", userInfoJson.get("id"));
			logger.info("--in UserInnerController.userDelete params={}",params);
			resultMap = userService.userDelete(params);
		} catch (Exception e) {
			logger.error("--error UserInnerController.userDelete,Exception{}",e);
			resultMap.put("code", Constants.ERROR_CODE);
			resultMap.put("msg", Constants.ERROR_MSG);
		}
		logger.info("--end UserInnerController.userDelete currentTimeMillis="+System.currentTimeMillis());
		renderJson(resultMap);
		
	}
	
}
